package com.wheelpicker.picker;

import android.os.Bundle;
import android.view.View;

import java.util.Calendar;

import com.wheelpicker.base.BasePickerDialog;
import com.wheelpicker.picker.datepicker.DatePickerConfig;
import com.wheelpicker.base.WheelType;
import com.wheelpicker.picker.datepicker.DateWheel;
import com.wheelpicker.picker.datepicker.WheelCalendar;

/**
 * Created by jzxiang on 16/4/19.
 */
public class DatePickerDialog extends BasePickerDialog<DatePickerConfig> {

    private DateWheel mDateWheel;
    private long mCurrentMillSeconds;


    public static DatePickerDialog newInstance(Builder builder) {
        DatePickerDialog datePickerDialog = new DatePickerDialog();
        Bundle bundle = getArgumentBundle(builder);
        datePickerDialog.setArguments(bundle);
        datePickerDialog.initialize(builder.mPickerConfig);
        return datePickerDialog;
    }

    protected void initialize(DatePickerConfig pickerConfig) {
        mPickerConfig = pickerConfig;
    }

    @Override
    protected void initCustomView(View view) {
        mDateWheel = new DateWheel(view, mPickerConfig);
    }

    @Override
    protected void onSureClicked() {
        Calendar calendar = Calendar.getInstance();
        // 五种选择模式，年月日时分，年月日，时分，月日时分，年月
        // ALL,YEAR_MONTH_DAY,HOURS_MINS,MONTH_DAY_HOUR_MIN,YEAR_MONTH, YEAR
        if (mPickerConfig.mType == WheelType.DateType.ALL) {
            calendar.set(Calendar.YEAR, mDateWheel.getCurrentYear());
            calendar.set(Calendar.MONTH, mDateWheel.getCurrentMonth() - 1);
            calendar.set(Calendar.DAY_OF_MONTH, mDateWheel.getCurrentDay());
            calendar.set(Calendar.HOUR_OF_DAY, mDateWheel.getCurrentHour());
            calendar.set(Calendar.MINUTE, mDateWheel.getCurrentMinute());
        } else if (mPickerConfig.mType == WheelType.DateType.YEAR_MONTH_DAY) {
            calendar.set(Calendar.YEAR, mDateWheel.getCurrentYear());
            calendar.set(Calendar.MONTH, mDateWheel.getCurrentMonth() - 1);
            calendar.set(Calendar.DAY_OF_MONTH, mDateWheel.getCurrentDay());
        } else if (mPickerConfig.mType == WheelType.DateType.HOURS_MINS) {
            calendar.set(Calendar.HOUR_OF_DAY, mDateWheel.getCurrentHour());
            calendar.set(Calendar.MINUTE, mDateWheel.getCurrentMinute());
        } else if (mPickerConfig.mType == WheelType.DateType.MONTH_DAY_HOUR_MIN) {
            calendar.set(Calendar.DAY_OF_MONTH, mDateWheel.getCurrentDay());
            calendar.set(Calendar.HOUR_OF_DAY, mDateWheel.getCurrentHour());
            calendar.set(Calendar.MINUTE, mDateWheel.getCurrentMinute());
        } else if (mPickerConfig.mType == WheelType.DateType.YEAR_MONTH) {
            calendar.set(Calendar.YEAR, mDateWheel.getCurrentYear());
            calendar.set(Calendar.MONTH, mDateWheel.getCurrentMonth() - 1);
        } else if (mPickerConfig.mType == WheelType.DateType.YEAR_MONTH) {
            calendar.set(Calendar.YEAR, mDateWheel.getCurrentYear());
        }
        mCurrentMillSeconds = calendar.getTimeInMillis();
        if (mDialogResultListener != null) {
            mDialogResultListener.result(this, mCurrentMillSeconds);
        }
    }

    public static class Builder extends BasePickDialogBuild<DatePickerConfig, Builder, DatePickerDialog> {

        public Builder() {
            mPickerConfig = new DatePickerConfig();
        }

        public Builder setType(int type) {
            mPickerConfig.mType = type;
            return this;
        }

        public Builder setMinMilliseconds(long milliseconds) {
            mPickerConfig.mMinCalendar = new WheelCalendar(milliseconds);
            return this;
        }

        public Builder setMaxMilliseconds(long milliseconds) {
            mPickerConfig.mMaxCalendar = new WheelCalendar(milliseconds);
            return this;
        }

        public Builder setCurrentMilliseconds(long milliseconds) {
            mPickerConfig.mCurrentCalendar = new WheelCalendar(milliseconds);
            return this;
        }

        public Builder setYearText(String year) {
            mPickerConfig.mYear = year;
            return this;
        }

        public Builder setMonthText(String month) {
            mPickerConfig.mMonth = month;
            return this;
        }

        public Builder setDayText(String day) {
            mPickerConfig.mDay = day;
            return this;
        }

        public Builder setHourText(String hour) {
            mPickerConfig.mHour = hour;
            return this;
        }

        public Builder setMinuteText(String minute) {
            mPickerConfig.mMinute = minute;
            return this;
        }

        @Override
        public DatePickerDialog build() {
            return newInstance(this);
        }

    }


}
